
<!-- 提示 -->
<div class="zhezhao_boxc" style="z-index:120;">
    <div class="opentcc"><p id="tips_content"></p></div>
</div>
<!--手机绑定-->
<div class="zhezhao_boxe">
    <div class="opentce">
        <div class="opentce_div0">手机绑定</div>
        <div class="dl_div1">
            <img src="__ROOT__/Public/images/shouji.png" class="dl_d1_img1">
            <input class="dl_d1_input1" type="text" id="mobile" placeholder="请输入手机号" autocomplete="off">
        </div>
        <div class="dl_div1" id="code_div">
            <img src="__ROOT__/Public/images/yanzhengma.png" class="dl_d1_img1">
            <input class="dl_d1_input2" type="text" id="code" placeholder="请输入验证码" autocomplete="off">
            <p class="dl_d1_p1" id="getcode" >获取验证码</p>
        </div>
        <div style="height:34px;"></div>
        <div class="opentce_divl" onclick="closebind();">取消</div>
        <div class="opentce_divr" id="bind" >确定</div>
    </div>
</div>
<script>
	var hid = "{$hid}";
	check_code();
	var is_code = 0;
    function check_code(){
    	$.post("{:U('Home/Mobile/Detail/checkSend')}",{'hid':hid},function(data){
    		if(data =='1'){
    			is_code = 1;
    			$("#code_div").show();
    		}else if(data == '2'){
    			is_code = 2;
    			$("#code_div").hide();
    		}
    	});
    }

	//	手机绑定
    function closebind(){
    	$(".zhezhao_boxe").hide();
    }
    
    $("#getcode").on("click",function(){
    	sendCode(this);
    });
    var clock = '';
	var nums = 60;
	var btn;
	function sendCode(thisBtn) {
		if(is_code != 1){
			$('#tips_content').html('无法发送，请联系管理员');
			$('.zhezhao_boxc').show();
			setTimeout(function(){
				$('.zhezhao_boxc').hide();
			},1000); 
			return false; 
		}
		var pattern = /^1\d{10}$/;
		var phone = $("#mobile").val();
		if(phone==''){
			$('#tips_content').html('请输入手机号！');
			$('.zhezhao_boxc').show();
			setTimeout(function(){
				$('.zhezhao_boxc').hide();
			},1000); 
			return false;
		}else{
			if(!pattern.test($.trim(phone))){
				$('#tips_content').html('请输入有效的手机号码！');
				$('.zhezhao_boxc').show();
				setTimeout(function(){
					$('.zhezhao_boxc').hide();
				},1000); 
				return false; 
			}
		}		  
		$.ajax({
			type: 'POST',
			url: "{:U('Home/Mobile/Detail/sendcode')}",
			data: {'phone':phone,'hid':hid},
			dataType: 'text',
			success: function(datas){
				if(datas=='1'){
					$('#tips_content').html('验证码发送成功！');
					$('.zhezhao_boxc').show();
					setTimeout(function(){
						$('.zhezhao_boxc').hide();
					},1000);
					btn = thisBtn;
					$(btn).off("click");
					btn.innerHTML = nums+'秒后重新获取';
					clock = setInterval(doLoop, 1000);
				}else{
					$('#tips_content').html(datas);
					$('.zhezhao_boxc').show();
					setTimeout(function(){
						$('.zhezhao_boxc').hide();
					},1000);
					return false; 
				}
			}	
		}); 
	}
	
	function doLoop() {
		nums--;
		if(nums > 0){
			btn.innerHTML = nums+'秒后重新获取'; 
		}else{
			clearInterval(clock); 
			$(btn).on("click",function(){
		    	sendCode(this);
		   });
			btn.innerHTML = '获取验证码';
			nums = 60; 
		}
	}
	
	$('#bind').click(function(){		
		var pattern = /^1\d{10}$/;
		
		var phone = $("#mobile").val();
		var code = $("#code").val();
		
		if(phone==''){
			$('#tips_content').html('请输入手机号！');
			$('.zhezhao_boxc').show();
			setTimeout(function(){
				$('.zhezhao_boxc').hide();
			},1000);
			return false;
		}else{
			if(!pattern.test($.trim(phone))){
				$('#tips_content').html('请输入有效的手机号码！');
				$('.zhezhao_boxc').show();
				setTimeout(function(){
					$('.zhezhao_boxc').hide();
				},1000);
				return false; 
			}
		}
		
		if(is_code == 1 && code==''){
			$('#tips_content').html('请输入验证码！');
			$('.zhezhao_boxc').show();
			setTimeout(function(){
				$('.zhezhao_boxc').hide();
			},1000);
			return false;
		}
					
	    $('#tips_content').html('绑定中~');
		$.ajax({
			type: 'POST',
			url: "{:U('Home/Mobile/Detail/dobind')}",
			data:{'phone':phone,'code':code,'hid':hid,'is_code':is_code},
			dataType: 'text',
			success: function(datas){
				$('.zhezhao_boxc').hide();	
				if(datas =='1'){
					$('#tips_content').html('绑定成功');
					$('.zhezhao_boxc').show();
					setTimeout(function(){
						$('.zhezhao_boxc').hide();
						location.reload();
					},1000);
					
				}else if(datas =='2'){
					$('#tips_content').html('绑定失败');
					$('.zhezhao_boxc').show();
					setTimeout(function(){
						$('.zhezhao_boxc').hide();
					},1000);
				}else{
					$('#tips_content').html(datas);
					$('.zhezhao_boxc').show();
					setTimeout(function(){
						$('.zhezhao_boxc').hide();
					},1000);
				}
				return false;
			}	
		});
		
	})
</script>